package com.team.bus.module.util;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import net.sf.jxls.exception.ParsePropertyException;  
import net.sf.jxls.transformer.XLSTransformer; 

public class Excelexport {



    public void autoexport(String templateFileName, String resultFileName,HttpServletRequest request,HttpServletResponse response,List<?> list1, List<?> list2, List<?> list3){


        //創建XLSTransformer對象
        XLSTransformer transformer = new XLSTransformer();
        //獲取java項目編譯後根路徑
        URL url = this.getClass().getClassLoader().getResource("");
        //得到模板文件路徑
        String srcFilePath =  templateFileName;
        // System.out.println("地址1:"+srcFilePath);
        Map<String,Object> beanParams = new HashMap<String,Object>();
        beanParams.put("list1", list1);
        beanParams.put("list2", list2);
        beanParams.put("list3", list3);


        String destFilePath = resultFileName;

        InputStream in=null;
        OutputStream out=null;
        //设置响应
        response.setHeader("Content-Disposition", "attachment;filename=" + resultFileName);
        response.setContentType("application/vnd.ms-excel");
        try {
            in=new BufferedInputStream(new FileInputStream(templateFileName));
            org.apache.poi.ss.usermodel.Workbook workbook=transformer.transformXLS(in, beanParams);
            out=response.getOutputStream();
            //将内容写入输出流并把缓存的内容全部发出去
            workbook.write(out);
            out.flush();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in!=null){try {in.close();} catch (IOException e) {}}
            if (out!=null){try {out.close();} catch (IOException e) {}}
        }
    }

	
    /** 
     * 根據模板生成Excel文件. 
     * @param templateFileName 模板文件.
     * @param resultFileName 生成的文件. 
     * @throws InvalidFormatException 
     */  
    public String createExcel(String templateFileName, String resultFileName, List<?> list1, List<?> list2, List<?> list3) throws InvalidFormatException{  
        //創建XLSTransformer對象  
        XLSTransformer transformer = new XLSTransformer();  
        //獲取java項目編譯後根路徑  
        URL url = this.getClass().getClassLoader().getResource("");  
        //得到模板文件路徑  
        String srcFilePath =  templateFileName;  
       // System.out.println("地址1:"+srcFilePath);
        Map<String,Object> beanParams = new HashMap<String,Object>();  
        beanParams.put("list1", list1);  
        beanParams.put("list2", list2);  
        beanParams.put("list3", list3);  
 
      
        String destFilePath = resultFileName;  
        //System.out.println("地址2:"+destFilePath);
        try {  
            //生成Excel文件  
            transformer.transformXLS(srcFilePath, beanParams, destFilePath);  
        } catch (ParsePropertyException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
        
        return destFilePath;
    } 
    
    public static  void aa(HttpServletRequest request){
    String aa=	request.getSession().getServletContext().getRealPath("/"); 
    System.out.println("路径"+aa);
    	
    }
		
	    public static void main(String[] args) {  
	    	

	    	
	    }  
		
	

}
